<?php

namespace App\Http\Controllers;

use App\Http\Requests\ShopAddressController\ShopAddressDestroyRequest;
use App\Http\Requests\ShopAddressController\ShopAddressStoreRequest;
use App\Http\Requests\ShopAddressController\ShopAddressUpdateRequest;
use App\Http\Resources\ShopAddressResource;
use App\Models\ShopAddress;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ShopAddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request  $request)
    {
        $user = Auth::user();
        $page = $request->get('page') ? $request->get('page') : 1;

        $shopAddress = ShopAddress::where('user_id', $user->id)->orderBy('id', 'desc')->paginate(20, $columns = ['*'], $pageName = 'page', $page);

        return ShopAddressResource::collection($shopAddress);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ShopAddressStoreRequest $request)
    {
        $user = Auth::user();
        $data = array_merge($request->all(), ['user_id' => $user->id]);
        $result = ShopAddress::create($data);
        if (! isset($result->id)) {
            return $this->error(500, '添加失败');
        }

        return $this->null();
    }

    public function update(ShopAddressUpdateRequest $request)
    {
        $user = Auth::user();
        $id = $request->input('id');
        $name = $request->input('name');
        $phone = $request->input('phone');
        $province = $request->input('province');
        $city = $request->input('city');
        $area = $request->input('area');
        $detailedAddress = $request->input('detailed_address');
        $postalCode = $request->input('postal_code');

        $shopAddress = ShopAddress::where('id', $id)->where('user_id', $user->id)->first();
        if (! $shopAddress) {
            return $this->error(500, '地址不存在');
        }

        $shopAddress->name = $name;
        $shopAddress->phone = $phone;
        $shopAddress->province = $province;
        $shopAddress->city = $city;
        $shopAddress->area = $area;
        $shopAddress->detailed_address = $detailedAddress;
        $shopAddress->postal_code = $postalCode;
        $result = $shopAddress->save();
        if (! $result) {
            return $this->error(500, '地址更新失败');
        }

        return $this->null();
    }

    public function destroy(ShopAddressDestroyRequest $request)
    {
        $user = Auth::user();
        $id = $request->input('id');

        $shopAddress = ShopAddress::where('id', $id)->where('user_id', $user->id)->first();
        if (! $shopAddress) {
            return $this->error(500, '地址不存在');
        }

        $result = $shopAddress->delete();
        if (! $result) {
            return $this->error(500, '删除失败');
        }

        return $this->null();
    }
}
